package cn.fintecher.gis.web;


import cn.fintecher.gis.enums.FlowType;
import cn.fintecher.gis.enums.Rapporttype;
import cn.fintecher.gis.model.MapSpotData;
import cn.fintecher.gis.model.request.BusinessWktRequest;
import cn.fintecher.gis.model.request.DateAnalysisWktRequest;
import cn.fintecher.gis.model.request.DetectionWktRequest;
import cn.fintecher.gis.model.response.FlowBusinessResponse;
import cn.fintecher.gis.service.DetectionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/detection")
@Api(value = "检测功能管理", description = "检测功能管理")
public class DetectionController {

    final static Map<String, String> dataMap = new HashMap<>();

    static {
        dataMap.put("A1", "行政办公用地");
        dataMap.put("A5", "医疗卫生用地");
        dataMap.put("A6", "社会福利用地");
        dataMap.put("A7", "文物古迹用地");
        dataMap.put("A21", "图书馆展览用地");
        dataMap.put("A22", "文化活动用地");
        dataMap.put("A33", "中小学用地");
        dataMap.put("A51", "医院用地");

        dataMap.put("B11", "零售商业用地");
        dataMap.put("B21", "金融保险用地");
        dataMap.put("B41", "加油加气站用地");
        dataMap.put("G1", "公园绿地");
        dataMap.put("G2", "防护绿地");
        dataMap.put("G3", "广场用地");
        dataMap.put("M1", "一类工业用地");
        dataMap.put("M2", "二类工业用地");
        dataMap.put("R21", "住宅用地");
        dataMap.put("R22", "服务设施用地");
        dataMap.put("S41", "公共交通场站用地");
        dataMap.put("S42", "社会停车场用地");
        dataMap.put("U13", "供燃气用地");
        dataMap.put("U15", "通信用地");
        dataMap.put("U22", "环卫用地");
    }

    private final Logger logger = LoggerFactory.getLogger(DetectionController.class);

    @Autowired
    private DetectionService detectionService;

    @PostMapping("/getDetectionWkt")
    @ApiOperation(value = "预警检测(区域对比)", notes = "预警检测(区域对比)")
    public ResponseEntity<List<MapSpotData>> getDetectionWkt(@Validated @RequestBody DetectionWktRequest request) throws Exception {
        List<MapSpotData> mapList = detectionService.getDetectionMap(request.getWkt(), request.getLayerCode(), request.getCql());
        for (MapSpotData mapSpotData : mapList) {
            Map<String, Object> attr = mapSpotData.getAttr();
            String code = (String) attr.get("TDYTQLXDM");
            attr.put("name", detectionService.getLandQuoName(code, Rapporttype.LAND_USE_AREA));
        }
        return ResponseEntity.ok().body(mapList);
    }


    @GetMapping("/getDetectionLayerCode")
    @ApiOperation(value = "预警检测（图层对比）", notes = "预警检测（图层对比）")
    public ResponseEntity<List<MapSpotData>> getDetectionLayerCode(@RequestParam(name = "deputyLayer") String layerCode1,
                                                                   @RequestParam(name = "mainLayer") String layerCode2,
                                                                   @RequestParam(value = "cql", required = false) String cql) throws Exception {
        List<MapSpotData> mapList = detectionService.getDetectionLayerCode(layerCode1, layerCode2, cql);

        for (MapSpotData mapSpotData : mapList) {
            Map<String, Object> attr = mapSpotData.getAttr();
            String code = (String) attr.get("TDYTQLXDM");
            attr.put("name", detectionService.getLandQuoName(code, Rapporttype.LAND_USE_AREA));
        }
        return ResponseEntity.ok().body(mapList);
    }


    @PostMapping("/getDateAnalysisWkt")
    @ApiOperation(value = "数据分析(区域对比)", notes = "数据分析(区域对比)")
    public ResponseEntity<List<MapSpotData>> getDateAnalysisWkt(@Validated @RequestBody DateAnalysisWktRequest request) throws Exception {
        String wkt = request.getWkt();
        String layerCode = request.getLayerCode();
        String cql = request.getCql();
        Rapporttype rapporttype = request.getRapporttype();

        List<MapSpotData> mapList = detectionService.getDetectionMap(wkt, layerCode, cql);
        for (MapSpotData mapSpotData : mapList) {
            Map<String, Object> map = mapSpotData.getAttr();
            String code = new String();
            if (rapporttype.equals(Rapporttype.LAND_CLASSIFICATION)) {
                code = (String) map.get("DLBM");
                map.put("name", detectionService.getLandQuoName(code, rapporttype));
            } else if (rapporttype.equals(Rapporttype.LAND_USE_AREA)) {
                code = (String) map.get("TDYTQLXDM");
                map.put("name", detectionService.getLandQuoName(code, rapporttype));
            } else if (rapporttype.equals(Rapporttype.LAND_CONSTRUCTION)) {
                code = (String) map.get("GZQLXDM");
                map.put("name", detectionService.getLandQuoName(code, rapporttype));
            } else if (rapporttype.equals(Rapporttype.REGULATORY_PLAN)) {
                code = (String) map.get("用地代码");
                String name = "未知";
                if (dataMap.containsKey(code)) {
                    name = dataMap.get(code);
                }
                map.put("name", name);
            } else { //房屋建筑图层
                map.put("name", "");
            }
        }
        return ResponseEntity.ok().body(mapList);
    }


    @GetMapping("/getDateAnalysisLayerCode")
    @ApiOperation(value = "数据分析（图层对比）", notes = "数据分析（图层对比）")
    public ResponseEntity<List<MapSpotData>> getDateAnalysisLayerCode(@RequestParam(name = "deputyLayer") String layerCode1,
                                                                      @RequestParam(name = "mainLayer") String layerCode2,
                                                                      @RequestParam(value = "cql", required = false) String cql,
                                                                      @RequestParam(name = "rapporttype") Rapporttype rapporttype) throws Exception {
        List<MapSpotData> mapList = detectionService.getDetectionLayerCode(layerCode1, layerCode2, cql);
        for (MapSpotData mapSpotData : mapList) {
            Map<String, Object> map = mapSpotData.getAttr();
            String code = new String();
            if (rapporttype.equals(Rapporttype.LAND_CLASSIFICATION)) {
                code = (String) map.get("DLBM");
                map.put("name", detectionService.getLandQuoName(code, rapporttype));
            } else if (rapporttype.equals(Rapporttype.LAND_USE_AREA)) {
                code = (String) map.get("TDYTQLXDM");
                map.put("name", detectionService.getLandQuoName(code, rapporttype));
            } else if (rapporttype.equals(Rapporttype.LAND_CONSTRUCTION)) {
                code = (String) map.get("GZQLXDM");
                map.put("name", detectionService.getLandQuoName(code, rapporttype));
            } else {
                code = (String) map.get("用地代码");
                String name = "未知";
                if (dataMap.containsKey(code)) {
                    name = dataMap.get(code);
                }
                map.put("name", name);
            }
        }
        return ResponseEntity.ok().body(mapList);
    }

    //FlowType
    @GetMapping("/getBusinessLayerCode")
    @ApiOperation(value = "业务分析（图层对比）", notes = "业务分析（图层对比）")
    public ResponseEntity<List<FlowBusinessResponse>> getBusinessLayerCode(@RequestParam(name = "flowType") FlowType flowType,
                                                                           @RequestParam(name = "layerCode") String layerCode) throws Exception {
        List<FlowBusinessResponse> results = detectionService.getBusinessLayerCode(flowType, layerCode);
        return ResponseEntity.ok().body(results);
    }

    @PostMapping("/getBusinessWkt")
    @ApiOperation(value = "业务分析（区域对比）", notes = "业务分析（区域对比）")
    public ResponseEntity<List<FlowBusinessResponse>> getBusinessWkt(@RequestBody BusinessWktRequest request) throws Exception {
        List<FlowBusinessResponse> results = detectionService.getBusinessWkt(request.getFlowType(), request.getWkt());
        return ResponseEntity.ok().body(results);
    }
}
